package com.users.rn.rncommon.ui;

import android.app.ActionBar;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;

import com.facebook.react.ReactActivity;
import com.facebook.react.ReactActivityDelegate;
import com.users.rn.rncommon.manager.ALHReactActivityDelegate;
import com.users.rn.rncommon.manager.ALHReactNativeHost;
import com.users.rn.rncommon.model.PluginInfo;
import com.users.rn.rncommon.util.RNPageTag;
import com.users.rn.rncommon.util.ReactConst;

/**
 * @Description:
 * @Author: bluce
 * @Date: 2020/12/3 10:35
 */
public class ReactBaseActivity extends ReactActivity implements RNPageTag {
    private ALHReactActivityDelegate mDelegate;

    private String pageTag;

    @Override
    protected ReactActivityDelegate createReactActivityDelegate() {
        mDelegate = new ALHReactActivityDelegate(this);
        return mDelegate;
    }

    protected void onDelegateCreate(ALHReactNativeHost host, PluginInfo pluginInfo) {
        mDelegate.onDelegateCreate(host, pluginInfo);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        hideActionBar();
        init(getIntent());
    }

    private void hideActionBar() {
        ActionBar actionBar = getActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
    }

    @Override
    public void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        init(intent);
    }

    private void init(Intent intent) {
        pageTag = intent != null ? intent.getStringExtra(ReactConst.INTENT_KEY_PAGE_TAG) : null;
    }

    /**
     * 加载页面
     */
    protected final void loadApp() {
        mDelegate.loadApp();
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (getReactNativeHost() != null && getReactNativeHost().hasInstance() && getReactNativeHost().getUseDeveloperSupport()) {
            //菜单键(只有长按才会回调此方法)，音量键
            if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
                getReactNativeHost().getReactInstanceManager().showDevOptionsDialog();
                return true;
            }
        }
        return super.onKeyUp(keyCode, event);
    }


    public ALHReactNativeHost getReactHost() {
        return mDelegate.getReactNativeHost();
    }


    @Override
    public String getPageTag() {
        return pageTag;
    }
}
